package com.hebeu.group.util;

/**
 * @author 言立慧
 * @date 2018/8/4 11:02
 *
 * 描述：自定义响应数据结构，这个类时提供给门户，IOS，安卓，微信商城使用的
 *      状态码：
 *          200：处理成功
 *          500：服务器内部错误，错误信息存放在msg字段内
 *          501：bean验证错误，不过多少个错误都已map形式返回
 *          502：服务器拦截到用户token错误
 *          555：异常信息抛出
 */

public class JSONResult {

    /**
     * 状态码信息
     */
    private Integer status;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 对象封装
     */
    private Object data;

    /**
     * 状态设置
     * @param status
     * @param msg
     * @param data
     * @return
     */
    public static JSONResult build(Integer status, String msg, Object data) {
        return new JSONResult(status, msg, data);
    }

    public static JSONResult ok(Object data) {
        return new JSONResult(data);
    }

    public static JSONResult ok() {
        return new JSONResult(null);
    }

    public static JSONResult errorMsg(String msg) {
        return new JSONResult(500, msg, null);
    }

    public static JSONResult errorMap(Object data) {
        return new JSONResult(501, "error", data);
    }

    public static JSONResult errorTokenMsg(String msg) {
        return new JSONResult(502, msg, null);
    }

    public static JSONResult errorException(String msg) {
        return new JSONResult(555, msg, null);
    }

    public JSONResult() {

    }

    public JSONResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public JSONResult(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == 200;
    }

    @Override
    public String toString() {
        return "JSONResult{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", object=" + data +
                '}';
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
